home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 135 (1990-05-15)(Ossowski, Stefan)(DE)(PD)[v Disaster Master 2].zip / Taifun 135 (1990-05-15)(Ossowski, Stefan)(DE)(PD)[v Disaster Master 2].adf / MiscUtils / Beep.c < prev    next >
C/C++ Source or Header  |  1990-02-23  |  7KB  |  241 lines

  1. /*====================================================*/
  2. /*                                                                                                        */
  3. /* Little gag to sound a real beep            V1.1                    */
  4. /* © J.Tyberghein                                                                            */
  5. /*        Tue Sep 26 08:43:30 1989 V1.0                                        */
  6. /*        Mon Dec 18 11:04:01 1989 V1.1                                        */
  7. /*        Tue Dec 19 09:21:03 1989                                                */
  8. /*                                                                                                        */
  9. /* compile with: (Lattice 5.0x)                                                */
  10. /*        lc -v -cms -O -j104 Beep                                                */
  11. /*        blink Beep.o lib lib:lc.lib        (no startupcode)    */
  12. /*                                                                                                        */
  13. /* this program is reexecutable and reentrable                */
  14. /*                                                                                                        */
  15. /*====================================================*/
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <devices/audio.h>
  20. #include <string.h>
  21.  
  22. #define VERSION                11
  23.  
  24. #define ERR_ALLOC            0
  25. #define ERR_OPEN            1
  26. #define ERR_CREATE        2
  27.  
  28. #define LEFT0F                1
  29. #define RIGHT0F                2
  30. #define RIGHT1F                4
  31. #define LEFT1F                8
  32.  
  33. #pragma libcall (rs->DOSBase) Write 30 32103
  34. #pragma libcall (rs->DOSBase) Output 3c 0
  35. #pragma libcall (rs->DOSBase) Exit 90 101
  36.  
  37. #pragma libcall Device BeginIO 1e 901
  38.  
  39. #pragma syscall AllocMem c6 1002
  40. #pragma syscall FreeMem d2 902
  41. #pragma syscall CloseLibrary 19e 901
  42. #pragma syscall OpenDevice 1bc 190804
  43. #pragma syscall CloseDevice 1c2 901
  44. #pragma syscall CheckIO 1d4 901
  45. #pragma syscall AbortIO 1e0 901
  46. #pragma syscall OpenLibrary 228 902
  47.  
  48. /*================================ Data ======================================*/
  49.  
  50. const char *PortName = "Sound Port";
  51.  
  52. /* Look for a left channel, then a right */
  53. UBYTE AllocationMap[] = { LEFT0F,LEFT1F,RIGHT0F,RIGHT1F };
  54.  
  55. /* This structure is needed to keep all global resources on the stack */
  56. /* So we need no global variables and our program is reentrable */
  57. struct Resources
  58.     {
  59.         struct IOAudio *id;                /* Sound ID */
  60.         UBYTE *SoundBuf;                    /* WaveForm Buffer */
  61.         LONG Len;                                    /* Sample length */
  62.         APTR DOSBase;
  63.     };
  64.  
  65. void CloseStuff (WORD Error, struct Resources *);
  66. void OpenStuff (struct Resources *);
  67. LONG Stol (char **);
  68. void Usage (struct Resources *);
  69. void Print (char *, struct Resources *);
  70. LONG SkipSpace (char **);
  71. struct IOAudio *PlaySound (struct IOAudio *,UBYTE *,ULONG,WORD,WORD,WORD);
  72. void Error (WORD, char *, struct Resources *);
  73.  
  74.  
  75. /*================================ Code ======================================*/
  76.  
  77. /*---------------------------- Main program ----------------------------------*/
  78.  
  79. LONG __saveds __asm myMain (register __a0 char *cmdline,register __d0 LONG Length)
  80. {
  81.     WORD i,Div;
  82.     LONG Rate;                                            /* Sample rate */
  83.     struct Resources rs;
  84.     char *ptr;
  85.  
  86.     if (!(rs->DOSBase = (APTR)OpenLibrary ("dos.library",0L))) return (100L);
  87.  
  88.     OpenStuff (&rs);
  89.  
  90.     ptr = cmdline;
  91.     if (SkipSpace (&ptr)) Usage (&rs);
  92.     if ((Rate = Stol (&ptr)) < 0L) Usage (&rs);
  93.     if (!SkipSpace (&ptr))
  94.         {
  95.             if ((rs.Len = Stol (&ptr)) < 0L) rs.Len = 4000L;
  96.         }
  97.     else rs.Len = 4000L;
  98.     if (rs.Len <= 125L || rs.Len >= 65536L) Usage (&rs);
  99.  
  100.     if (!(rs.SoundBuf = (UBYTE *)AllocMem (rs.Len,MEMF_CHIP | MEMF_CLEAR)))
  101.         Error (ERR_ALLOC,"SoundBuf",&rs);
  102.     Div = rs.Len/125;
  103.     for (i=0 ; i<rs.Len ; i++)
  104.         (rs.SoundBuf)[i] = (i%2) ? 127 : 127+(i-rs.Len)/Div;
  105.     rs.id = PlaySound (rs.id,rs.SoundBuf,rs.Len,1,(WORD)(3579545L/Rate),64);
  106.     while (!CheckIO (rs.id)) ;
  107.     AbortIO (rs.id);
  108.  
  109.     CloseStuff (0,&rs);
  110. }
  111.  
  112. /*----------------------- Skip spaces in a string ----------------------------*/
  113.  
  114. LONG SkipSpace (char **s)
  115. /* return TRUE if eoln */
  116. {
  117.     while (**s && (**s == ' ' || **s == 0xa)) (*s)++;
  118.     return (!**s);
  119. }
  120.  
  121. /*------------------- Convert a string to an integer -------------------------*/
  122.  
  123. LONG Stol (char **s)
  124. /* return -1 if error */
  125. {
  126.     LONG n;
  127.  
  128.     for (n=0L ; **s && **s != ' ' && **s != 0xa ; (*s)++)
  129.         {
  130.             if (**s > '9' || **s < '0') return (-1L);
  131.             n = n*10L+**s-'0';
  132.         }
  133.     return (n);
  134. }
  135.  
  136. /*-------------------------------- Usage -------------------------------------*/
  137.  
  138. void Usage (struct Resources *rs)
  139. {
  140.     Print ("Usage : Beep <Rate> [<Length>]\n",rs);
  141.     Print ("   <Rate>   : SampleRate\n",rs);
  142.     Print ("   <Length> : Length of sample (125<Length<65536) (default 4000)\n",rs);
  143.     CloseStuff (0,rs);
  144. }
  145.  
  146. /*-------------------------- Close everything --------------------------------*/
  147.  
  148. void CloseStuff (WORD Error, struct Resources *rs)
  149. {
  150.     if (rs->id)
  151.         {
  152.             if (rs->id->ioa_Request.io_Device) CloseDevice (rs->id);
  153.             if (rs->id->ioa_Request.io_Message.mn_ReplyPort)
  154.                 DeletePort (rs->id->ioa_Request.io_Message.mn_ReplyPort);
  155.             FreeMem (rs->id,(LONG)sizeof (struct IOAudio));
  156.         }
  157.     if (rs->SoundBuf) FreeMem (rs->SoundBuf,rs->Len);
  158.     CloseLibrary ((struct Library *)(rs->DOSBase));
  159.     Exit ((LONG)Error);
  160. }
  161.  
  162. /*----------------------- Print message on screen ----------------------------*/
  163.  
  164. void Print (char *mes, struct Resources *rs)
  165. {
  166.     Write (Output (),mes,(long)strlen (mes));
  167. }
  168.  
  169. /*----------------------- Error handling routine -----------------------------*/
  170.  
  171. void Error (WORD Error, char *Object, struct Resources *rs)
  172. {
  173.     char *Head;
  174.  
  175.     switch (Error)
  176.         {
  177.             case ERR_CREATE:    Head = "creating"; break;
  178.             case ERR_OPEN:        Head = "opening"; break;
  179.             case ERR_ALLOC:        Head = "allocating"; break;
  180.         }
  181.     Print ("ERROR: ",rs);
  182.     Print (Head,rs);
  183.     Print (" ",rs);
  184.     Print (Object,rs);
  185.     Print (" !\n",rs);
  186.     CloseStuff (Error,rs);
  187. }
  188.  
  189. /*----------------------------- Open stuff -----------------------------------*/
  190.  
  191. void OpenStuff (struct Resources *rs)
  192. {
  193.     struct MsgPort *Port;
  194.  
  195.     rs->id = NULL;
  196.     rs->SoundBuf = NULL;
  197.     rs->Len = 0;
  198.  
  199.     if (!(rs->id = (struct IOAudio *)AllocMem ((LONG)sizeof (struct IOAudio),MEMF_PUBLIC | MEMF_CLEAR)))
  200.         Error (ERR_ALLOC,"IOAudio",rs);
  201.     rs->id->ioa_Request.io_Message.mn_Node.ln_Pri = 10;
  202.  
  203.     if (!(Port = (struct MsgPort *)CreatePort (PortName,0L)))
  204.         Error (ERR_CREATE,PortName,rs);
  205.     rs->id->ioa_Request.io_Message.mn_ReplyPort = Port;
  206.     rs->id->ioa_Data = AllocationMap;
  207.     rs->id->ioa_Length = sizeof (AllocationMap);
  208.  
  209.     if (OpenDevice (AUDIONAME,0L,rs->id,0L))
  210.         {
  211.             rs->id->ioa_Request.io_Device = NULL;
  212.             Error (ERR_OPEN,AUDIONAME,rs);
  213.         }
  214.     rs->id->ioa_Request.io_Flags = ADIOF_PERVOL;
  215.     rs->id->ioa_Request.io_Command = CMD_WRITE;
  216.     rs->id->ioa_Period = 3579545L/8000;
  217.     rs->id->ioa_Volume = 64;
  218.     rs->id->ioa_Cycles = 1;
  219. }
  220.  
  221. /*---------------------------- Play a sound ----------------------------------*/
  222.  
  223. struct IOAudio *PlaySound (struct IOAudio *ioa,UBYTE *Buffer,ULONG BufLen,
  224.                                                     WORD Repeat,WORD Period,WORD Volume)
  225. {
  226.     struct Device *Device;
  227.  
  228.     ioa->ioa_Request.io_Flags = ADIOF_PERVOL;
  229.     ioa->ioa_Request.io_Command = CMD_WRITE;
  230.     ioa->ioa_Period = Period;
  231.     ioa->ioa_Volume = Volume;
  232.     ioa->ioa_Cycles = Repeat;
  233.     ioa->ioa_Length = BufLen;
  234.     ioa->ioa_Data = Buffer;
  235.     Device = ioa->ioa_Request.io_Device;
  236.     BeginIO (ioa);
  237.     return (ioa);
  238. }
  239.  
  240. /*=============================== The end ====================================*/
  241.